package com.jye.rseven.user.serivce.impl;

import org.springframework.stereotype.Service;
import com.jye.rseven.domain.Sharing;
import com.jye.rseven.service.SharingService;
import com.jye.rseven.user.mapper.SharingMapper;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.UUID;

@Service
public class SharingServiceImpl implements SharingService {

    @Autowired
    SharingMapper sharingMapper;

    @Override
    public Sharing findSharingByuId(int uId) {
        return sharingMapper.findSharingById(uId);
    }

    @Override
    public List<Sharing> findAllSharing(int skip, int limit) {
        return sharingMapper.findAllSharing(skip, limit);
    }

    @Override
    public String addSharing(Sharing sharing) {
        try{
            //判断有无加入
            int inSharing = sharingMapper.isInSharing(sharing.getUId(),sharing.getCId());
            if(inSharing!=0){
                //有记录
                return "加入失败，你已经加入过了";
            }else{
                //无记录,插入
                //利用UUID生成分享码
                String uuId = UUID.randomUUID().toString();
                String[] split = uuId.split("-");
                StringBuilder s  = new StringBuilder();
                for (String s1 : split) {
                    s.append(s1);
                }
                //转换成String
                String shareCode =  s.toString();
                sharing.setShareCode(shareCode);
                sharingMapper.addSharing(sharing);
                return "加入成功!";
            }
        }catch (Exception e){
            return "用户加入失败!";
        }
    }

    @Override
    public int updateSharing(Sharing sharing) {
        try{
            sharingMapper.updateSharing(sharing);
            return 1;
        }catch (Exception e){
            return 0;
        }
    }

//    @Override
//    public String findSharingStatusById(int id) {
//        return sharingMapper.findSharingStatusById(id);
//    }

    @Override
    public Sharing findSharingByShareCode(String shareCode) {

        Sharing sharing = sharingMapper.findSharingByShareCode(shareCode);
        return sharing;

    }

    @Override
    public String findShareCodeByuIdAndCId(int uId, int cId) {
        String shareCode=sharingMapper.findShareCodeByuIdAndCId(uId, cId);
        return shareCode;

    }
}
